package com.yzt.minemodule.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yzt.zhuju.mvp.MVPActivity;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.utils.FormatUtils;
import com.yzt.minemodule.R;
import com.yzt.zhuju.utils.NumberFomateUtils;

import java.util.Objects;

/**
 * 提现成功
 */
@SuppressLint({"NonConstantResourceId", "SetTextI18n"})
public class WithDrawActivity extends MVPActivity {

    TextView tv_bank_card;

    EditText et_money;

    TextView tv_money;

    TextView tv_with_all;

    TextView tv_rate;

    TextView tv_service_charge;

    private String extraMoney;

    private double rate = 0;

    public static void startSelf(Activity activity, String money, String account, double rate) {
        activity.startActivity(new Intent(activity, WithDrawActivity.class)
                .putExtra("extra_money", money)
                .putExtra("extra_account", account)
                .putExtra("extra_rate", rate));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_withdraw;
    }

    @Override
    public void installViews() {
        mTitleBar.setTitle("提现");
        extraMoney = getIntent().getStringExtra("extra_money");
        String extraAccount = getIntent().getStringExtra("extra_account");
        rate = getIntent().getDoubleExtra("extra_rate", 0);

        tv_bank_card = findViewById(R.id.tv_bank_card);
        tv_rate = findViewById(R.id.tv_rate);
        tv_service_charge = findViewById(R.id.tv_service_charge);
        tv_money = findViewById(R.id.tv_money);
        et_money = findViewById(R.id.et_money);

        tv_bank_card.setText(extraAccount);
        if (extraAccount == null || extraAccount.equals("")) {
            tv_bank_card.setText("暂未绑定银行卡");
        } else {
            tv_bank_card.setText(extraAccount);
        }

        tv_rate.setText("费率：" + FormatUtils.formatKeepOne(rate * 100) + "%");

        tv_service_charge.setText("服务费：0");

        tv_money.setText("当前余额：￥" + extraMoney + "元");

        et_money.addTextChangedListener(new TextWatcher() {

            private boolean isEditing;

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int i1, int i2) {
                if (isEditing) {
                    // 避免递归调用
                    return;
                }

                String originalText = s.toString();

                // 移除所有非数字和小数点的字符
                String formattedText = originalText.replaceAll("[^\\d.]", "");

                // 如果出现多个小数点，保留第一个，并删除其它的
                int decimalSeparatorIndex = formattedText.indexOf(".");
                if (decimalSeparatorIndex != -1) {
                    formattedText = formattedText.substring(0, decimalSeparatorIndex + 1) +
                            formattedText.substring(decimalSeparatorIndex + 1).replaceAll("\\.", "");
                }

                // 限制小数点后的位数为两位
                if (decimalSeparatorIndex != -1) {
                    int decimalCount = formattedText.length() - decimalSeparatorIndex - 1;
                    if (decimalCount > 2) {
                        formattedText = originalText.substring(0, start);
                    }
                }

                // 如果第一个字符是小数点，在前面补上一个零
                if (formattedText.length() > 0 && formattedText.charAt(0) == '.') {
                    formattedText = "0" + formattedText;
                }

                // 更新输入框的内容
                try {
                    isEditing = true;
                    et_money.setText(formattedText);
                    et_money.setSelection(formattedText.length());
                } finally {
                    isEditing = false;
                }
                String inputText = et_money.getText().toString().trim();
                if (inputText.equals("")) {
                    tv_service_charge.setText("服务费：0");
                    return;
                }
                double total = Double.parseDouble(inputText);
                double recharge = total * rate;
                tv_service_charge.setText("服务费：" + FormatUtils.formatKeepTwo(recharge));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        findViewById(R.id.tv_with_all).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_money.setText(extraMoney);
            }
        });
        findViewById(R.id.tv_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 提现
                String money = et_money.getText().toString().trim();

                if (money.equals("")) {
                    showToast("请输入金额");
                    return;
                }

                putPageData();
            }
        });
    }

    /**
     * 执行转账
     */
    private void putPageData() {
        String money = et_money.getText().toString().trim();

        BizLayer.getInstance().getMineModule().withDraw(money, new AresSubscriber<Objects>() {
            @Override
            protected void onSuccess(Objects wallet) {
                showToast("提现成功！");
                finish();
            }
        });
    }
}
